<?php 
	require_once("config.php");
	
	// kui ei ole sisse logitud
	if(!isSet($_SESSION["id"])){
		header("Location: login.php");
	}
	
	// Kasutaja on sisse loginud
	$yo = new Yo($mysqli, $_SESSION["id"]);
	
	// saadame sõnumi
	if(isSet($_REQUEST["email"])){
		$notice = $yo->sendYo($_REQUEST["email"]);
		
		if($notice == ""){
			echo "edukalt saadetud";
		}else {
			echo $notice;
		}
	}
	
	// küsime kõik lugemata yo'd
	$yos = $yo->getUnreadYos();
	
	//print_r($yos);
	
	if(isSet($_REQUEST["yo_id"])){
		
		// märgmine andmebaasis loetuks, lisame loetud kellaaja
		$yo->readYo($_REQUEST["yo_id"]);
		
		
		$alert_string = $_REQUEST['sender']." saatis Yo ".$_REQUEST['sent'];
		echo "
			<script>
				alert('$alert_string');
				window.location = 'home.php';
			</script>
		";
	}
	
	
	
	
	// kasutaja tahab välja logida
	if(isSet($_REQUEST["logout"])){
		session_destroy();
		header("Location: login.php");
	}
?> 
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Home</title>
</head>
<body>
	
	<h1>Tere tulemast <?=$_SESSION["name"];?></h1>
	<a href="?logout">Log out</a>
	<br><br><br><br>
	
	<form action="home.php">
		<input type="email" name="email">
		<input type="submit" value="Yo">
	</form>
	
	<br>
	
	<h2>Minule saadetud</h2>
	<?php 
		foreach($yos as $y){
			echo "<a href='?yo_id=$y->id&sender=$y->email&sent=$y->sent'>Yo</a><br>";
		}
	?>
	
</body>
</html>